package com.example.musicrecommend.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.musicrecommend.common.ErrorCode;
import com.example.musicrecommend.exception.BusinessException;
import com.example.musicrecommend.exception.ThrowUtils;
import com.example.musicrecommend.mapper.CommentMapper;
import com.example.musicrecommend.model.entity.Comment;
import com.example.musicrecommend.model.vo.CommentVO;
import com.example.musicrecommend.service.CommentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
* @author PERSON
* @description 针对表【comment(评论)】的数据库操作Service实现
* @createDate 2024-10-03 23:26:32
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService{

    @Override
    public void validComment(Comment comment, boolean isAdd) {
        ThrowUtils.throwIf(comment==null, ErrorCode.PARAMS_ERROR);

        Long id = comment.getId();
        Long userId = comment.getUserId();
        Long songId = comment.getSongId();
        String content = comment.getContent();


        if (isAdd) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(content), ErrorCode.PARAMS_ERROR);
            ThrowUtils.throwIf(userId<=0 || songId<=0, ErrorCode.PARAMS_ERROR);
        } else {
            ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        }

        if (StringUtils.isNotBlank(content) && content.length() > 256) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "评论内容过长");
        }

    }

    @Override
    public Page<CommentVO> selectCommentPageBySongId(Page<CommentVO> page, Long songId) {
        return this.getBaseMapper().selectCommentPageBySongId(page,songId);
    }
}




